package com.scott.classicthreadmodal.demo1.producer;

import com.scott.classicthreadmodal.Product;
import com.scott.classicthreadmodal.demo1.storage.Storage;

/**
 * 生产者
 */
public class Producer implements Runnable {

    /**
     * 生产者名字
     */
    private String name;

    /**
     * 仓库
     */
    private Storage storage;

    public Producer(String name, Storage storage) {
        this.name = name;
        this.storage = storage;
    }


    /**
     * 生产
     */
    @Override
    public void run() {
        try {
            while (true) {
                Product product = new Product((int) (Math.random() * 100));
                System.out.printf(Thread.currentThread().getName()+":%s准备生产(%d)\n", this.name, product.getId());
                storage.push(product);
                System.out.printf(Thread.currentThread().getName()+":%s生产完成(%d)\n", this.name, product.getId());
                System.out.println(Thread.currentThread().getName()+":===============");
                Thread.sleep(2000);

            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
